package com.dlc.felear.xiaoensale.activity;

import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.IoItemAdapter;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.customView.LoadingDialog;
import com.dlc.felear.xiaoensale.customView.SingleInputDialog;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.itdlc.android.library.base.BaseActivity;
import com.dlc.felear.xiaoensale.deviceControlSf.NewDeviceControl;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.Map;

import butterknife.BindView;

/**
 * 设备保养页面
 */
public class MainTainPageNewActivity extends BaseActivity implements Handler.Callback ,SingleInputDialog.SingleDialogCallback{
    private static final String TAG = "MainTainPageNewActivity";
    private TopbarHelper topbarHelper;

    @BindView(R.id.mt_gv1)
    GridView outputIoGv;
    @BindView(R.id.mt_gv)
    GridView inputIoGv;
    private IoItemAdapter inputIoItemAdapter;
    private IoItemAdapter outputIoItemAdapter;
    private LoadingDialog loadingDialog;
    private Handler handler;
    private SingleInputDialog singleInputDialog;
    @Override
    protected int getLayoutRes() {
        return R.layout.activity_main_tain_new_layout;
    }

    @Override
    protected void initialView() {
        EventBus.getDefault().register(this);
        topbarHelper = new TopbarHelper(this);
        topbarHelper.setTitle("设备保养页面");
        topbarHelper.setcon(R.drawable.arrow_left);
        topbarHelper.setNavActivity(MotorPagerActivity.class);
        inputIoItemAdapter=new IoItemAdapter(this,20);
        inputIoItemAdapter.setPreName("X");
        outputIoItemAdapter=new IoItemAdapter(this,30);
        outputIoItemAdapter.setPreName("Y");
        inputIoGv.setAdapter(inputIoItemAdapter);
        outputIoGv.setAdapter(outputIoItemAdapter);
        handler=new Handler(this);
        singleInputDialog=new SingleInputDialog(this,this);
        singleInputDialog.setEtInputType(InputType.TYPE_CLASS_NUMBER);
        singleInputDialog.setHintMsg("输入打开的时长（0-255）");
        singleInputDialog.setTips("时长");
        outputIoGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                singleInputDialog.showDialog(position+1);
            }
        });
    }



    @Override
    protected void initData() {
    }

    @Subscriber(tag = Constant.ACTION_INPUT_IO_INFO_RESULT)
    public void recieveInputIoResult(Map<Integer, Integer> data) {
        Log.e(TAG,JsonHelper.toJson(data));
        inputIoItemAdapter.setItems(data);
    }
    @Subscriber(tag = Constant.ACTION_OUTPUT_IO_INFO_RESULT)
    public void recieveOutputIoResult(Map<Integer, Integer> data) {
//        Log.e(TAG, "recieveOutputIoResult="+JsonHelper.toJson(data));
        outputIoItemAdapter.setItems(data);
    }

    @Override
    public boolean handleMessage(Message msg) {
        closeLoadDialog();
        return false;
    }

    private void  closeLoadDialog(){
        if(loadingDialog!=null){
            loadingDialog.close();
        }
    }
    private void  showLoadDialog(){
        if(loadingDialog==null){
            loadingDialog=new LoadingDialog(this,"正在执行...");
        }
        loadingDialog.show();
        handler.sendEmptyMessageDelayed(1211,1000);
    }


    @Override
    public void SingleDialogInputResult(String value, int id) {
        try {
            NewDeviceControl.getDeviceControlHelper(MainTainPageNewActivity.this).openOutPutIo( id,Integer.parseInt(value));
            showLoadDialog();
        }catch (Exception e){
            showShortToast("请输入合法的数字");
        }
    }
}